<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/7
 * Time: 1:40 PM
 */

namespace App\BO\OrderStatus\State;

use App\BO\OrderStatus\Events\OrderDistributedEvent;
use App\BO\OrderStatus\Events\OrderServiceFinishEvent;
use App\BO\OrderStatus\Events\OrderServiceStartEvent;
use App\BO\OrderStatus\FlowTrait\AdminApplyRefundTrait;
use App\BO\OrderStatus\OrderStateContext;
use App\BO\OrderStatus\State\Report\ReportedState;
use App\BO\OrderStatus\State\Service\ServicingState;

class WaitingServiceState extends AOrderState
{
    use AdminApplyRefundTrait;

    protected $desc = '待上门';

    /**
     * 有服务信息
     * @return bool
     */
    public function hasServiceInfo()
    {
        return true;
    }

    /**
     * 可以转派
     * @return bool
     */
    public function couldReDispatch()
    {
        return true;
    }

    /**
     * @inheritDoc
     */
    public function couldReport()
    {
        return true;
    }

    /**
     * 可以由服务端发起退单
     * @return bool
     */
    public function couldAdminApplyRefund()
    {
        return true;
    }

    /**
     * @inheritDoc
     */
    public function report()
    {
        $this->orderStateModifyStore->toReported();
        $this->stateContext->setOrderState(OrderStateContext::getInstance(ReportedState::class));
    }

    /**
     * 服务
     * @deprecated
     * @return mixed|void
     * @throws \Exception
     */
    public function service()
    {
        $this->orderStateModifyStore->toWaitingComment();
        // 服务完成，将状态设置为待评价
        $this->stateContext->setOrderState(OrderStateContext::getInstance(WaitingCommentState::class));

        event(new OrderServiceFinishEvent($this->stateContext->getOrder()));
    }

    /**
     * 开始服务
     */
    public function serviceStart()
    {
        $this->orderStateModifyStore->toServicing();
        // 服务完成，将状态设置为待评价
        $this->stateContext->setOrderState(OrderStateContext::getInstance(ServicingState::class));

        event(new OrderServiceStartEvent($this->stateContext->getOrder()));
    }

    /**
     * 分配操作
     * @return mixed|void
     * @throws \Exception
     */
    public function distribute()
    {
        $this->orderStateModifyStore->toWaitingService();
        // 将状态设置为待上门服务
        $this->stateContext->setOrderState(OrderStateContext::getInstance(WaitingServiceState::class));

        event(new OrderDistributedEvent($this->stateContext->getOrder()));  // 订单已经分配过了，待服务
    }
}
